import {createRouter,createWebHashHistory} from  'vue-router'
import * as auth from '../util/auth' 
import hospitalapi from '../api'

const islogin=async function(to,from){
    //获取token
    let token=auth.getToken();
    console.log(token + "=====")
//验证token是否有效
let result =await hospitalapi.get("/api/checktoken");
if(!token||result.code!=200){
return{path:'/login'};
}

}

const router=createRouter(
    {
     history:createWebHashHistory(),
     routes:[
        {path:'/',redirect:"/hospital"},
        {path:'/hospital',
            component:()=>import("../views/hospital.vue"),
            children:[{path:'/caseslist',component:()=>import("../views/caseslist.vue")},
                {path:'/userlist',component:()=>import("../views/userlist.vue")},
                {path:'/patientlist',component:()=>import("../views/patientlist.vue")},
                {path:'/doctorlist',component:()=>import("../views/doctorlist.vue")},
                {path:'/bedlist',component:()=>import("../views/bedlist.vue")},
                {path:'/wardlist',component:()=>import("../views/wardlist.vue")},
                {path:'/departmentlist',component:()=>import("../views/departmentlist.vue")},
                {path:'/personlist',component:()=>import("../views/personlist.vue")},
                {path:'/echartslist',component:()=>import("../views/echartslist.vue")}
            ],
            
            beforeEnter:islogin,
            beforeEnter:islogin
        },
        {path:'/login',
          component:()=>import("../views/login.vue")
            
        },
        {
            path: '/register',
            component: () => import('../views/register.vue') 
          }
        
     ]
}
);

export default router;
